﻿using System;
using System.IO;
using System.Net;
using System.Threading;
using WMPLib;

namespace Voize
{


	internal class CInternetStream
	{


		private string m_Url;
		private string m_DisplayString;
		private WindowsMediaPlayer m_Player;
		private bool m_IsPlaying = false;
        private bool m_IsMuted = false;


		internal bool IsPlaying
		{
			get { return m_IsPlaying; }
		}


		internal string Url
		{
			get { return m_Url; }
		}



		internal string DisplayString
		{
			get { return m_DisplayString; }
		}



        internal bool Play(string file, CVolume volume_handler)
		{
			try
			{
				string url;

                if (!file.StartsWith("file://"))
                {
                    if (file.StartsWith("http://") == false)
                        file = "http://" + file;

                    if (file.EndsWith(".pls") || file.EndsWith(".m3u"))
                        url = this.ParsePlaylist(file);
                    else
                        url = file;
                }
                else
                    url = this.ParsePlaylist(file);

				m_Url = url;
				m_DisplayString = m_Url;

                WindowsMediaPlayer player = new WindowsMediaPlayerClass();
				m_Player = player;

				if (url == "")
					throw new Exception("Parsing failed.");

				player.URL = url;				
				player.controls.play();

				m_IsPlaying = true;
                volume_handler.SetStreamSoundHandler();

				return true;
			}
			catch (Exception error)
			{
				throw new Exception(error.Message);
			}

		}


		internal void SetUrl(string file)
		{
			string url;

            if (!file.StartsWith("file://"))
            {
                if (file.StartsWith("http://") == false)
                    file = "http://" + file;

                if (file.EndsWith(".pls") || file.EndsWith(".m3u"))
                    url = this.ParsePlaylist(file);
                else
                    url = this.ParsePlaylist(file);
            }
            else
                url = this.ParsePlaylist(file);

			m_Url = url;
			m_DisplayString = m_Url;

		}



		internal bool Stop()
		{
			bool retval;

			if (m_Player != null)
			{
				m_Player.controls.stop();
				retval = true;
			}
			else
				retval = false;

			m_IsPlaying = false;

			return retval;
		}



		private string ParsePlaylist(string PlsFile)
		{
			string line;
			string url;
            StreamReader reader;

            if (PlsFile.StartsWith("file://"))
            {
                reader = new StreamReader(PlsFile.Remove(0,7));
            }
            else
            {
                reader = new StreamReader(this.GetTemporaryCopy(PlsFile));
            }

			url = "";

			while ((line = reader.ReadLine()) != null)
			{
				if (line.ToLower().StartsWith("file1="))
				{
					url = line.Split('=')[1];
					break;
				}
			}
			
			return url;
		}




		private string GetTemporaryCopy(string PlsFile)
		{
			string TempPath;
			WebClient client = new WebClient();

			TempPath = Environment.GetEnvironmentVariable("TEMP") + "\\voize.tmp";

			if(File.Exists(TempPath))
				File.Delete(TempPath);

			client.DownloadFile(PlsFile,TempPath);

			return TempPath;
		}


        internal void SetVolume(int value)
        {
            value *= 10;
            m_Player.settings.volume = value;
            if (m_IsMuted == true)
                m_IsMuted = false;

        }


        internal void Mute()
        {
            if (m_IsMuted == false)
            {
                m_Player.settings.mute = true;
                m_IsMuted = true;
            }
            else
            {
                m_Player.settings.mute = false;
                m_IsMuted = false;
            }
        }


        internal bool IsMuted
        {
            get { return m_IsMuted; }
        }


	}
}
